package main

import (
	"fmt"
	"strconv"
)

/**
执行用时：0 ms, 在所有 Go 提交中击败了100.00%的用户
内存消耗：2.8 MB, 在所有 Go 提交中击败了6.40%的用户
 */
func addBinary(a string, b string) string {

	a, b = reverse_string(a), reverse_string(b)
	res := ""
	for i, t := 0, 0; i < len(a) || i < len(b) || t != 0; i++ {
		if i < len(a) { t += stoi(a[i:i+1]) }
		if i < len(b) { t += stoi(string(b[i])) }
		res += to_string(t % 2)
		t /= 2
	}
	res = reverse_string(res)
	return res
}

// 翻转字符串，返回翻转后的结果
func reverse_string(s string) string {
	runes := []rune(s)

	for from, to := 0, len(runes)-1; from < to; from, to = from + 1, to - 1 {
		runes[from], runes[to] = runes[to], runes[from]
	}

	return string(runes)
}

// stoi
func stoi(s string) int {
	res, _ := strconv.Atoi(s)
	return res
}

// to_string
func to_string(val int) string {
	return strconv.Itoa(val)
}

func main() {

	fmt.Println(addBinary("111", "1"))  // 1000
}
